package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.client.LoginUserClient;
import cn.itsource.hrm.constants.VerificationConstant;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.domain.VipUser;
import cn.itsource.hrm.exception.ValidUtils;
import cn.itsource.hrm.mapper.VipUserMapper;
import cn.itsource.hrm.service.IVipUserService;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.BitStatesUtils;
import cn.itsource.hrm.vo.VipUserVo;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2021-02-26
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VipUserMapper vipUserMapper;

    @Autowired
    private LoginUserClient loginUserClient;
    @Override
    public void register(VipUserVo vipUserVo) {
        //参数校验
        //1 null校验
        ValidUtils.assertNotNull(vipUserVo.getMobile(), "请输入手机号！");
        ValidUtils.assertNotNull(vipUserVo.getPassword(), "请输入密码！");
        String inputSmsCode = vipUserVo.getSmsCode();
        ValidUtils.assertNotNull(inputSmsCode, "请输入短信验证码！");
        //2 短信验证码
        String key = VerificationConstant.USER_REG + ":" + vipUserVo.getMobile();
        Object obj = redisTemplate.opsForValue().get(key);
        ValidUtils.assertNotNull(obj, "验证码以过期，请重新刷新页面！");
        String smsCode = ((String) obj).split(":")[0]; //code：time
        ValidUtils.assertEqualIgnoreCase(inputSmsCode, smsCode, "请输入正确短信验证码！");
        //3 手机没有注册
        List<VipUser> vipUsers = vipUserMapper
                .selectList(new EntityWrapper<VipUser>().eq("phone", vipUserVo.getMobile()));
        ValidUtils.assertListNull(vipUsers, "用户已存在，不能重复注册");
        //保存数据库
        //2 授权中心：登录信息表,授权中心加密密码，我们直接获取就是加密
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(vipUserVo.getMobile());
        loginUser.setPassword(vipUserVo.getPassword());
        loginUser.setType(1);
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        if (ajaxResult.isSuccess()) {
            Long longUserId = Long.valueOf(ajaxResult.getResultObj().toString());
            //1 用户基本信息表-你认为是问题就是问题（在employee或user设置冗余字段密码），如果你认为不是（在employee或user不要设置冗余字段密码）。
            VipUser vipUser = new VipUser();
            vipUser.setCreateTime(new Date().getTime());
            vipUser.setPhone(vipUserVo.getMobile());
            vipUser.setPassword(vipUserVo.getPassword());
            vipUser.setNickName(vipUserVo.getMobile());
            vipUser.setLoginId(longUserId);

            //位运算表示状态
            long state = BitStatesUtils.OP_REGISTED; //注册
            state = BitStatesUtils.addState(state, BitStatesUtils.OP_ACTIVED); //激活
            state = BitStatesUtils.addState(state, BitStatesUtils.OP_AUTHED_PHONE); //手机已认证
            vipUser.setBitState(state);
            vipUserMapper.insert(vipUser);
        }

    }
}
